# 康托尔集，size表示线段长度，n表示n分集，l表示行数，x0和y0是初始位置
import turtle


def cantor(x0, y0, size, n, l):
    if l > 0:
        turtle.penup()
        turtle.goto(x0, y0)
        turtle.pendown()
        turtle.goto(x0 + size, y0)
        for i in range(n // 2 + 1):
            x = x0 + (size / n * i * 2)
            y = y0 - 20
            cantor(x, y, size / n, n, l - 1)


def main():
    turtle.title("康托尔集")
    turtle.setup(900, 600, 200, 100)
    turtle.pensize(5)
    turtle.tracer(2)
    turtle.hideturtle()
    turtle.speed(0)
    x0 = -400
    y0 = 200
    size = 600
    n = 3
    l = 5
    cantor(x0, y0, size, n, l)
    turtle.done()


main()
